﻿using AE_BEPUPhysics_Addition;
using Aspose.Cells.Drawing;
using BEPUphysics.BroadPhaseEntries;
using BEPUphysics.Character;
using BEPUphysics.CollisionTests.CollisionAlgorithms;
using FixMath.NET;
using GameConfig.ships;
using GameConfig.weapon.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using TEngine;
using TMPro;
using UnityEditor.Graphs;
using UnityEngine;
using UnityEngine.EventSystems;
using static UnityEngine.Networking.UnityWebRequest;

namespace GameLogic
{
    internal class LaserStrategy : FireStrategy
    {
        protected List<LineRenderer> m_Lasers = new List<LineRenderer>();
        protected List<ParticleSystem> m_LaserBoomEndObj = new List<ParticleSystem>();
        int m_LaserNumber = 1;
        int m_LaserInstence = 0;
        float m_Width = 0.3f;
        public override void Init(WeaponControl weaponControl, ShipSlotControl slotControl)
        {
            base.Init(weaponControl, slotControl);
            for (int i = 0; i < m_LaserNumber; ++i)
            {
                var laserObj = new GameObject();
                var laser = laserObj.AddComponent<LineRenderer>();
                m_Lasers.Add(laser);
                laser.alignment = LineAlignment.TransformZ;
                laser.textureMode = LineTextureMode.DistributePerSegment;
                laser.positionCount = 2;
                laser.numCapVertices = 4;
                laser.startWidth = 1;
                laser.endWidth = 1;
                laser.gameObject.SetActive(false);
                //laser.startColor = m_StartColor;
                //laser.endColor = m_EndColor;
                laser.useWorldSpace = true;
                laser.material = Resources.Load<Material>("Materials/Effects/Laser");
                laser.material.color = new Vector4(1, 1, 1, 1);
                laser.sortingLayerName = "Item";
                //laser.transform.SetParent(slot.transform, false);

                var endObj = PoolManager.Instance.GetGameObject("");
                if (endObj)
                {
                    var LaseBoomEndObj = endObj.GetComponent<ParticleSystem>();
                    LaseBoomEndObj.transform.SetParent(laser.transform);
                    LaseBoomEndObj.transform.localScale = new Vector3(1, 1, 1);
                    LaseBoomEndObj.transform.localPosition = new Vector3(0, 0, 0);
                    m_LaserBoomEndObj.Add(LaseBoomEndObj);
                }
                //IsDamage.Add(true);
            } 
        }
        BEPUutilities.Vector3 m_WeaponPos;
        UnityEngine.Vector3 m_EndPos;
        public override void Fire(BEPUutilities.Vector3 weaponPos)
        {
            base.Fire(weaponPos);
            m_WeaponPos = weaponPos;
            BEPUphysics.RayCastResult result = new BEPUphysics.RayCastResult();
            if (FightLogicManager.Instance.Raycast(new BEPUutilities.Ray(m_WeaponPos, m_WeaponControl.WeaponData.Direction), m_WeaponControl.WeaponData.Cfg.ShotRange / 1000m,RayCastFilter, out result))
            {
                var collidable = result.HitObject as Collidable;
                var shipControl = ShipControl.TryGetShipControl(collidable.UnityColliderID);
                shipControl.BeDamaged(m_WeaponControl.WeaponData.Cfg.Damage);
            }
            m_LaserInstence += 3;
            m_LaserInstence = Mathf.Min(m_LaserInstence, SystemModule.LogicFrame);
        }

        public override void Clear()
        {

        }
        bool RayCastFilter(BEPUphysics.BroadPhaseEntries.BroadPhaseEntry intersectObj)
        {
            var collidable = intersectObj as Collidable;
            var shipControl = ShipControl.TryGetShipControl(collidable.UnityColliderID);
            var influence = m_SlotControl.GetShipInfluence();

            if (shipControl && shipControl != m_SlotControl.GetShipControl())
                return true;

            return false;
        }
        public override void Update()
        {
            m_EndPos = m_WeaponControl.ViewWeaponPos + m_WeaponControl.WeaponData.Direction.ToFloat() * (m_WeaponControl.WeaponData.Cfg.ShotRange / 1000);
            BEPUphysics.RayCastResult result = new BEPUphysics.RayCastResult();
            if (m_LaserInstence > 0 && FightLogicManager.Instance.Raycast(new BEPUutilities.Ray(m_WeaponControl.ViewWeaponPos.ToFix64(), m_WeaponControl.WeaponData.Direction), m_WeaponControl.WeaponData.Cfg.ShotRange / 1000m, RayCastFilter, out result))
            {
                var collidable = result.HitObject as Collidable;
                var shipControl = ShipControl.TryGetShipControl(collidable.UnityColliderID);

                m_EndPos = result.HitData.Location.ToFloat();
            }

            foreach (var i in m_Lasers)
            {
                i.gameObject.SetActive(m_LaserInstence > 0);
                i.SetPosition(0, m_WeaponControl.ViewWeaponPos);
                i.SetPosition(1, m_EndPos);

                var bilibili =  m_LaserInstence / (float)(SystemModule.LogicFrame);
                i.startWidth = m_Width * 1.3f * bilibili;
                i.endWidth = m_Width * 0.8f * bilibili;
            }
        }
        public override void DoLogic()
        {
            if(m_LaserInstence > 0  && !m_WeaponControl.WeaponData.IsShotCooling)
                m_LaserInstence--;

        }
    }
}
